﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mscook.core.domain;
using mscook.core.services;

namespace mscook.core.components
{
    public class ActionBonusStrategy : AbstractBonusStrategy
    {
        private IList<ActionBonusSetting> actionBonusSettings;
        private string actionName;
        private LoginUser loginUser;
        private ILoginUserService loginUserService;
        public ActionBonusStrategy(string actionName,IList<ActionBonusSetting> actionBonusSettings,LoginUser loginUser,ILoginUserService loginUserService)
        {
            this.actionName = actionName;
            this.actionBonusSettings = actionBonusSettings;
            this.loginUser = loginUser;
            this.loginUserService = loginUserService;
        }
        #region IBonusStrategy 成员

        protected override bool IfNeedBonus()
        {
            return true;
        }

        public override bool CheckEnoughBonus(int num)
        {
            return true;
        }

        public override bool CheckEnoughBonus()
        {
            return true;
        }



        protected override int GetAvailableBonus()
        {
            var item = actionBonusSettings.Where(x => x.ActionName.ToLower() == actionName.ToLower()).FirstOrDefault();
            if (item != null)
            {
                return item.Bonus;
            }
            else
            {
                return 0;
            }
        }

        protected override bool UpdateUserBonus(int bonus, int num)
        {
            return true;
        }

        protected override bool UpdateUserBonus(int bonus)
        {
           ResetLoginUser();
            if (loginUser.TodayBonus > 0)
            {
                this.loginUser.Bonus += bonus;
                loginUser.TodayBonus -= bonus;
                loginUserService.SaveOrUpdate(this.loginUser);
                return true;
            }
            return false;
        }

        private void ResetLoginUser()
        {
            if (DateTime.Compare(loginUser.LastLogin.Date,DateTime.Now.Date)<=0)
            {
                //TimeSpan ts = DateTime.Now - loginUser.LastLogin;
                loginUser.LastLogin =DateTime.Now.AddDays(1);
                loginUser.TodayBonus += 20;
            }
        }

        #endregion
    }
}
